package com.msb.mall.product.service.impl;

import com.msb.mall.product.service.CategoryBrandRelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.Query;

import com.msb.mall.product.dao.CategoryDao;
import com.msb.mall.product.entity.CategoryEntity;
import com.msb.mall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    /*
     * 查询所有的数据
     * 将数据封装为树形数据
     * */
    @Override
    public List<CategoryEntity> queryPageTree(Map<String, Object> params) {
        //1 查询所有数据
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);


        //2 将数据封装为树形数据
        //第一步遍历出所有的大类 parent_id = 0
        List<CategoryEntity> list = categoryEntities.stream().
                filter(categoryEntity -> {
                    return categoryEntity.getParentCid() == 0;
                }).map(categoryEntity -> {
            //根据大类找到所有的小类，递归的方式实现
            categoryEntity.setChildren(getCategoryChildren(categoryEntity, categoryEntities));
            return categoryEntity;
        }).sorted((entity1, entity2) -> {
            return ((entity1.getSort() == null ? 0 : entity1.getSort()) - (entity2.getSort() == null ? 0 : entity2.getSort()));
        }).collect(Collectors.toList());


        return list;
    }

    /*
     * 逻辑删除
     * */
    @Override
    public void removeCategroyByIds(List<Long> ids) {
        // 1.检查类别数据是否在其它业务中使用

        // 2.逻辑删除
        baseMapper.deleteBatchIds(ids);
    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        List<Long> parentPath = findParentPath(catelogId, paths);
        Collections.reverse(parentPath);

        return parentPath.toArray(new Long[parentPath.size()]);

    }

    @Override
    public void updateDetail(CategoryEntity category) {
        this.updateById(category);
        if (!StringUtils.isEmpty(category.getName())) {
            //同步更新级联的数据
            categoryBrandRelationService.updateCatelogName(category.getCatId(), category.getName());
        }

    }

    /**
     * 递归查询数据
     *
     * @param catelogId
     * @param paths
     * @return
     */
    private List<Long> findParentPath(Long catelogId, List<Long> paths) {
        paths.add(catelogId);
        CategoryEntity entity = this.getById(catelogId);
        if (entity.getParentCid() != 0) {
            findParentPath(entity.getParentCid(), paths);
        }

        return paths;
    }

    /*
     * 查找该大类中的所有小类 递归查找
     * categoryEntity 某个大类
     *categoryEntities 所有的类别数据
     * */
    private List<CategoryEntity> getCategoryChildren(CategoryEntity categoryEntity, List<CategoryEntity> categoryEntities) {

        List<CategoryEntity> collect = categoryEntities.stream().filter(entity -> {
            //根据大类找到直属的小类
            return entity.getParentCid().equals(categoryEntity.getCatId());
        }).map(entity -> {
            //根据小类找到对应的 下属类
            entity.setChildren(getCategoryChildren(entity, categoryEntities));
            return entity;
        }).sorted((entity1, entity2) -> {
            return ((entity1.getSort() == null ? 0 : entity1.getSort()) - (entity2.getSort() == null ? 0 : entity2.getSort()));
        }).collect(Collectors.toList());

        return collect;
    }


}
